package com.mass.waybill.scheduleTypes.service.impl;

import com.mass.core.utils.AjaxResponse;
import com.mass.waybill.order.model.WbShippingOrder;
import com.mass.waybill.scheduleTypes.dao.WbScheduleTypesMapper;
import com.mass.waybill.scheduleTypes.model.WbScheduleTypes;
import com.mass.waybill.scheduleTypes.model.WbScheduleTypesExample;
import com.mass.waybill.scheduleTypes.service.WbScheduleTypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Author: ZSX
 * @Description: 费用明细  service 接口  实现类
 */
@Service
@Transactional
public class WbScheduleTypesServiceImpl implements WbScheduleTypesService {

    @Autowired
    private WbScheduleTypesMapper wbScheduleTypesMapper;

    @Override
    public AjaxResponse findSupplement(WbShippingOrder wbShippingOrder, Integer page, Integer limit) {
        List<WbShippingOrder> wbShippingOrderList = wbScheduleTypesMapper.findSupplement(wbShippingOrder, (page - 1) * limit, limit);
        long count = wbScheduleTypesMapper.findSupplementCount(wbShippingOrder);
        return AjaxResponse.success("ok", wbShippingOrderList, count, page);
    }

    /**
     * 费用统计 分页
     *
     * @param wbShippingOrder
     * @param page
     * @param limit
     * @return
     */
    @Override
    public AjaxResponse costStatistics(WbShippingOrder wbShippingOrder, Integer page, Integer limit) {
        List<WbShippingOrder> wbShippingOrderList = wbScheduleTypesMapper.costStatistics(wbShippingOrder, (page - 1) * limit, limit);
        long count = wbScheduleTypesMapper.costStatisticsCount(wbShippingOrder);
        return AjaxResponse.success("ok", wbShippingOrderList, count, page);
    }

    /**
     * 费用统计 查询所有 不分页
     *
     * @param wbShippingOrder
     * @return
     */
    @Override
    public List<WbShippingOrder> costStatisticsAll(WbShippingOrder wbShippingOrder,Integer page,Integer limit) {
        List<WbShippingOrder> wbShippingOrderList = wbScheduleTypesMapper.costStatistics(wbShippingOrder, page, limit);
        return wbShippingOrderList;
    }

    @Override
    @Transactional
    public int deleteSupplement(String batchNumber) {
        return wbScheduleTypesMapper.deleteSupplementByBatchNumber(batchNumber.trim());
    }

    /**
     * 更新消费明细    多行数据，采用先删后加的方式更新数据
     *
     * @param wbScheduleTypesList
     * @param states              0  订单更新费用明细    1 更新增补费用明细
     * @param orderCode           订单编码
     * @return
     */
    @Override
    @Transactional
    public int replaceData(List<WbScheduleTypes> wbScheduleTypesList, int states, String orderCode, String batchNumber) {
        //获取辅助类
        WbScheduleTypesExample wbScheduleTypesExample = new WbScheduleTypesExample();
        WbScheduleTypesExample.Criteria criteria = wbScheduleTypesExample.createCriteria();
        criteria.andStatesEqualTo(states);
        criteria.andOrderCodeEqualTo(orderCode);
        if (!ObjectUtils.isEmpty(batchNumber)) {
            criteria.andBatchNumberEqualTo(batchNumber.trim());
        }
        //删除
        wbScheduleTypesMapper.deleteByExample(wbScheduleTypesExample);
        if (!ObjectUtils.isEmpty(wbScheduleTypesList) && wbScheduleTypesList.size() > 0) {
            //批量添加
            wbScheduleTypesMapper.insertBatch(wbScheduleTypesList);
            return 1;
        }
        return 0;
    }

    @Override
    public List<WbScheduleTypes> findConditions(WbScheduleTypes wbScheduleTypes) {
        WbScheduleTypesExample wbScheduleTypesExample = new WbScheduleTypesExample();
        WbScheduleTypesExample.Criteria criteria = wbScheduleTypesExample.createCriteria();
        if (!ObjectUtils.isEmpty(wbScheduleTypes)) {
            if (!ObjectUtils.isEmpty(wbScheduleTypes.getOrderCode())) {
                criteria.andOrderCodeEqualTo(wbScheduleTypes.getOrderCode().trim());
            }
            if (!ObjectUtils.isEmpty(wbScheduleTypes.getStates())) {
                criteria.andStatesEqualTo(wbScheduleTypes.getStates());
            }
            if (!ObjectUtils.isEmpty(wbScheduleTypes.getBatchNumber())) {
                criteria.andBatchNumberEqualTo(wbScheduleTypes.getBatchNumber().trim());
            }

            if (!ObjectUtils.isEmpty(wbScheduleTypes.getReviewStatus())) {
                if (wbScheduleTypes.getReviewStatus() == -1) {
                    criteria.andReviewStatusIsNull();
                } else {
                    criteria.andReviewStatusIsNotNull();
                }
            }
        }
        return wbScheduleTypesMapper.selectByExample(wbScheduleTypesExample);
    }

    @Override
    public int supplementaryAudit(WbScheduleTypes wbScheduleTypes) {
        WbScheduleTypesExample wbScheduleTypesExample = new WbScheduleTypesExample();
        WbScheduleTypesExample.Criteria criteria = wbScheduleTypesExample.createCriteria();
        criteria.andBatchNumberEqualTo(wbScheduleTypes.getBatchNumber().trim());
        return wbScheduleTypesMapper.updateByExampleSelective(wbScheduleTypes, wbScheduleTypesExample);
    }
}
